<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="航班编号" prop="flightno">
        <el-input
          v-model="queryParams.flightno"
          placeholder="请输入航班编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发日期" prop="departdate">
        <el-date-picker clearable
          v-model="queryParams.departdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出发日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出发城市" prop="departurecityname">
        <el-input
          v-model="queryParams.departurecityname"
          placeholder="请输入出发城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到达城市" prop="arrivecityname">
        <el-input
          v-model="queryParams.arrivecityname"
          placeholder="请输入到达城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-show="$store.state.user.name == 'admin'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:amend:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:amend:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:amend:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:amend:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="amendList" @selection-change="handleSelectionChange">
      <el-table-column label="航班编号" align="center" prop="flightno" />
      <el-table-column label="飞机型号" align="center" prop="aircrafttype" />
      <el-table-column label="出发日期" align="center" prop="departdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出发时间" align="center" prop="departtime" />
      <el-table-column label="到达时间" align="center" prop="arrivetime" />
      <el-table-column label="出发城市" align="center" prop="departurecityname" />
      <el-table-column label="到达城市" align="center" prop="arrivecityname" />
      <el-table-column label="商务舱价格" align="center" prop="vipPrice" />
      <el-table-column label="普通舱价格" align="center" prop="comPrice" />
      <el-table-column label="剩余商务舱" align="center" prop="vipNumChange" />
      <el-table-column label="剩余普通舱" align="center" prop="comNumChange" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="ticketId">
          <el-input v-model="form.ticketId" />
        </el-form-item>
        <el-form-item label="编号" prop="flightid">
          <el-input v-model="form.flightid" />
        </el-form-item>
        <el-form-item label="乘客姓名" prop="pname">
          <el-input v-model="form.pname" placeholder="请输入乘客姓名" />
        </el-form-item>
        <el-form-item label="航班编号" prop="fno">
          <el-input v-model="form.fno" placeholder="请输入航班编号" />
        </el-form-item>
        <el-form-item label="飞机舱" prop="level">
          <el-select v-model="form.level" placeholder="请选择飞机舱"  clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in levelOptions" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出发时间" prop="dtime">
          <el-input v-model="form.dtime" placeholder="请输入出发时间" />
        </el-form-item>
        <el-form-item label="出发城市" prop="dcity">
          <el-input v-model="form.dcity" placeholder="请输入出发城市" />
        </el-form-item>
        <el-form-item label="到达时间" prop="atime">
          <el-input v-model="form.atime" placeholder="请输入到达时间" />
        </el-form-item>
        <el-form-item label="到达城市" prop="acity">
          <el-input v-model="form.acity" placeholder="请输入到达城市" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price"/>
        </el-form-item>
        <el-form-item label="剩余座位" prop="seat">
          <el-input v-model="form.seat"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="reserve">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAmend, getAmend, delAmend, addAmend, updateAmend } from "@/api/system/amend";
import {alterTickets} from "@/api/system/tickets";

export default {
  name: "Amend",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单修改表格数据
      amendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flightno: null,
        aircrafttype: null,
        departdate: null,
        departtime: null,
        arrivetime: null,
        departurecityname: null,
        arrivecityname: null,
        vipNum: null,
        comNum: null,
        vipPrice: null,
        comPrice: null,
        vipNumChange: null,
        comNumChange: null
      },
      // 表单参数
      form: {
        level:''
      },
      // 表单校验
      rules: {
        level: [{
          required: true,
          message: '请选择飞机舱',
          trigger: 'change'
        }],
      },
      levelOptions: [{
        "label": "商务舱",
        "value": "商务舱"
      }, {
        "label": "经济舱",
        "value": "经济舱"
      }]
    };
  },
  created() {
    // 从路由中取参数
    this.ticketId = this.$route.query.id||null;
    // 赋值给查询参数
    this.queryParams.ticketId = this.ticketId;
    this.getList();
  },
  watch:{
    $route() {
      this.ticketId = this.$route.query && this.$route.query.id;
    },
    'form.level':function (val){
      if (val==="商务舱"){
        this.form.price=this.form.vipPrice
        this.form.seat=this.form.vipNumChange
      }else {
        this.form.price=this.form.comPrice
        this.form.seat=this.form.comNumChange
      }
    }
  },

  methods: {
    /** 查询订单修改列表 */
    getList() {
      this.loading = true;
      listAmend(this.queryParams).then(response => {
        this.amendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        flightid: null,
        flightno: null,
        aircrafttype: null,
        departdate: null,
        departtime: null,
        arrivetime: null,
        departurecityname: null,
        arrivecityname: null,
        vipNum: null,
        comNum: null,
        vipPrice: null,
        comPrice: null,
        vipNumChange: null,
        comNumChange: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.flightid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单修改";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const flightid = row.flightid || this.ids
      getAmend(flightid).then(response => {
        this.form = response.data;
        this.form.ticketId=this.ticketId;
        this.form.pname=this.$store.state.user.name;
        this.form.fno=this.form.flightno;
        this.form.ddate=this.form.departdate;
        this.form.dtime=this.form.departtime;
        this.form.dcity=this.form.departurecityname;
        this.form.atime=this.form.arrivetime;
        this.form.acity=this.form.arrivecityname;
        this.open = true;
        this.title = "修改订单修改";
      });
    },
    reserve() {
      const ticketId = this.ticketId
      this.$refs["form"].validate(valid => {
        if (valid) {
          alterTickets(this.form,ticketId).then(response => {
            this.$modal.msgSuccess("改签成功");
            this.open = false;
            this.getList();
            return response;
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.flightid != null) {
            updateAmend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAmend(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const flightids = row.flightid || this.ids;
      this.$modal.confirm('是否确认删除订单修改编号为"' + flightids + '"的数据项？').then(function() {
        return delAmend(flightids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/amend/export', {
        ...this.queryParams
      }, `amend_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
